package cn.afactory.tygz.ui.activities;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import cn.afactory.tygz.R;
import cn.afactory.tygz.db.DBManager;
import cn.afactory.tygz.model.MoneyShop;
import cn.afactory.tygz.model.UserInfo;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.base.BaseActivity;
import cn.afactory.tygz.ui.base.BaseFragmentActivity;
import cn.afactory.tygz.ui.base.utils.DialogUtil;
import cn.afactory.tygz.ui.base.utils.LoadingDialog;
import cn.afactory.tygz.ui.base.utils.ToastUtils;

/**
 * Created by xi on 15/10/11.
 */
public class ShopExchangeActivity extends BaseFragmentActivity {


    private ImageView imageView;
    private TextView name_tv;
    private TextView money_tv;
    private TextView des_tv;
    private  UserInfo userInfo;
    private   MoneyShop shop;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.ac_shop_exchange);
        setTitleText("商品兑换");
        userInfo = DBManager.getInstance(getApplicationContext()).getDefaultUserInfo();

        setRightButton(mContext.getString(R.string.label_btn_exchange_in), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doExchange();
            }
        });

        initViews();
        initData();
    }

    private void initViews(){

        imageView = (ImageView) findViewById(R.id.imageView);
        name_tv = (TextView) findViewById(R.id.name_tv);
        money_tv = (TextView) findViewById(R.id.money_tv);
        des_tv = (TextView) findViewById(R.id.des_tv);
    }

    private void initData(){

        shop = (MoneyShop) getIntent().getSerializableExtra("shop");
        if(shop != null){
            BaseImgView.loadimg(imageView, shop.pic, R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small, R.drawable.ic_pic_loading_default_small, ImageScaleType.EXACTLY,
                    250,
                    250,
                    0);

            name_tv.setText(shop.name);
            money_tv.setText("金币:"+shop.money);
            if(shop.des.length() == 0){
                des_tv.setText("暂无描述");
            }else{
                des_tv.setText(shop.des);
            }

        }else{

            //finish();
        }
    }

    private LoadingDialog loadingDialog;
    protected void showLoadingDialog(String msg) {
        if (loadingDialog == null) {
            loadingDialog = DialogUtil.showLoadingDialog(this, msg, true);
        }
        if (!TextUtils.isEmpty(msg)) {
            loadingDialog.setDlgMessage(msg);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    protected void hideLoadingDialog() {
        if (!isFinishing() && loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
    private void doExchange(){


        int money = userInfo.money;
        if(money < shop.money){

            ToastUtils.show(this,"金币不足，无法兑换哦!");
            return;
        }

        showLoadingDialog("兑换中...");
        NetManager.getInstance(this).doGetExchange(userInfo.id, shop.id, new OnResponseListener<Integer>() {
            @Override
            public void onComplete(boolean isOK, Integer result, int errorCode, String errorMsg) {

                userInfo.money = result;
                DBManager.getInstance(getApplicationContext()).saveUserInfo(userInfo);
                hideLoadingDialog();
                ToastUtils.show(mContext, "兑换成功!");

            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                hideLoadingDialog();
                ToastUtils.show(mContext,errorMessage);
            }
        });

    }
}
